export interface CacheConfig {
  enabled: boolean;
  localTTL: number;  // 本地缓存TTL（秒）
  redisTTL: number;  // Redis缓存TTL（秒）
  maxSize: number;   // 本地缓存最大条目数
  useRedis: boolean;
  redisUrl?: string;
}

export interface BatchConfig {
  enabled: boolean;
  maxBatchSize: number;
  timeout: number;    // 批处理超时时间（毫秒）
  retryCount: number;
  retryDelay: number; // 重试延迟（毫秒）
}

export interface MonitorConfig {
  enabled: boolean;
  sampleInterval: number;  // 采样间隔（毫秒）
  historySize: number;     // 历史记录大小
  alertThresholds: {
    cpu: number;           // CPU使用率阈值（百分比）
    memory: number;        // 内存使用率阈值（百分比）
    errorRate: number;     // 错误率阈值（百分比）
    responseTime: number;  // 响应时间阈值（毫秒）
  };
}

export interface DatabaseConfig {
  maxConnections: number;
  minConnections: number;
  connectionTimeout: number;
  queryTimeout: number;
  indexedFields: string[];
}

export interface PerformanceConfig {
  cache: CacheConfig;
  batch: BatchConfig;
  monitor: MonitorConfig;
  database: DatabaseConfig;
}

// 默认配置
const defaultConfig: PerformanceConfig = {
  cache: {
    enabled: true,
    localTTL: 300,       // 5分钟
    redisTTL: 3600,      // 1小时
    maxSize: 1000,
    useRedis: false
  },
  batch: {
    enabled: true,
    maxBatchSize: 1000,
    timeout: 5000,       // 5秒
    retryCount: 3,
    retryDelay: 1000    // 1秒
  },
  monitor: {
    enabled: true,
    sampleInterval: 5000,  // 5秒
    historySize: 1000,     // 保存1000条历史记录
    alertThresholds: {
      cpu: 80,             // 80%
      memory: 90,          // 90%
      errorRate: 5,        // 5%
      responseTime: 2000   // 2秒
    }
  },
  database: {
    maxConnections: 10,
    minConnections: 2,
    connectionTimeout: 10000,  // 10秒
    queryTimeout: 5000,       // 5秒
    indexedFields: [
      'userId',
      'createdAt',
      'status',
      'type'
    ]
  }
};

// 环境特定配置
const environmentConfigs: Record<string, Partial<PerformanceConfig>> = {
  development: {
    cache: {
      enabled: true,
      localTTL: 60,
      redisTTL: 3600,
      maxSize: 1000,
      useRedis: false
    },
    monitor: {
      enabled: true,
      sampleInterval: 10000,  // 10秒
      historySize: 1000,
      alertThresholds: {
        cpu: 90,              // 更宽松的阈值
        memory: 95,
        errorRate: 10,
        responseTime: 5000
      }
    }
  },
  production: {
    cache: {
      enabled: true,
      localTTL: 300,
      redisTTL: 3600,
      maxSize: 5000,
      useRedis: true,
      redisUrl: process.env.REDIS_URL
    },
    batch: {
      enabled: true,
      maxBatchSize: 5000,    // 更大的批处理大小
      timeout: 3000,         // 更短的超时时间
      retryCount: 3,
      retryDelay: 1000
    },
    monitor: {
      enabled: true,
      sampleInterval: 5000,  // 更频繁的采样
      historySize: 1000,
      alertThresholds: {
        cpu: 75,             // 更严格的阈值
        memory: 85,
        errorRate: 3,
        responseTime: 1500
      }
    },
    database: {
      maxConnections: 20,    // 更多的连接数
      minConnections: 5,
      connectionTimeout: 8000,
      queryTimeout: 3000,    // 更短的查询超时
      indexedFields: [
        'userId',
        'createdAt',
        'status',
        'type'
      ]
    }
  },
  test: {
    cache: {
      enabled: false,        // 测试环境禁用缓存
      localTTL: 60,
      redisTTL: 300,
      maxSize: 100,
      useRedis: false
    },
    batch: {
      enabled: false,       // 测试环境禁用批处理
      maxBatchSize: 100,
      timeout: 1000,
      retryCount: 1,
      retryDelay: 500
    },
    monitor: {
      enabled: false,      // 测试环境禁用监控
      sampleInterval: 1000,
      historySize: 100,
      alertThresholds: {
        cpu: 95,
        memory: 95,
        errorRate: 20,
        responseTime: 10000
      }
    }
  }
};

// 获取当前环境的配置
export const getCurrentConfig = (): PerformanceConfig => {
  const env = process.env.NODE_ENV || 'development';
  const envConfig = environmentConfigs[env] || {};
  
  return mergeConfigs(defaultConfig, envConfig);
};

// 合并配置
const mergeConfigs = (
  base: PerformanceConfig,
  override: Partial<PerformanceConfig>
): PerformanceConfig => {
  return {
    cache: { ...base.cache, ...override.cache },
    batch: { ...base.batch, ...override.batch },
    monitor: {
      ...base.monitor,
      ...override.monitor,
      alertThresholds: {
        ...base.monitor.alertThresholds,
        ...override.monitor?.alertThresholds
      }
    },
    database: { ...base.database, ...override.database }
  };
};

// 导出配置实例
export const performanceConfig = getCurrentConfig();
